package com.yc.goods.biz;

import com.yc.goods.dao.GoodsVODao;
import com.yc.vo.GoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsVOBizImpl implements GoodsVOBiz{
    @Autowired
    private GoodsVODao goodsVODao;
    /**
     * 分页分组查询数据
     * @param tno
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public Map<String, Object> findByPage(Integer tno, Integer page, Integer pagesize){
        Map<String, Object> map = new HashMap<>();
        int pagenum = (pagesize * (page -1));
        //获取第page页的数据
        try {
            List<GoodsVO> list = goodsVODao.findByPage(tno, pagenum, pagesize);
            for (int i = 0; i < list.size(); i++) {
                String[] arr = list.get(i).getPics().split(";");
                if(null != arr && arr.length > 0){
                    list.get(i).setPics(arr[0]);
                }
            }
            //获取分页的总条数
            int total = goodsVODao.totalPage(tno);
            map.put("goods", list);
            map.put("total", total);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 根据类型编号统计数据总量
     * @param tno
     * @return
     */
    @Override
    public int totalPage(Integer tno) {
        int list = goodsVODao.totalPage(1);
        System.out.println(list);
        return list;
    }
}
